/*
 * 单片机初始化
 */
#include "head\MCU.h"
#include "driverlib\MSP430F5xx_6xx\driverlib.h"

void msp430_Init()
{
    //Init WDT
    WDT_A_initWatchdogTimer(WDT_A_BASE,
        WDT_A_CLOCKSOURCE_ACLK,//选用ACLK32.768kHz
        WDT_A_CLOCKDIVIDER_512K);//16s溢出
//    WDT_A_initWatchdogTimer(WDT_A_BASE,
//        WDT_A_CLOCKSOURCE_VLOCLK,//选用VLOCLK 10kHz
//        WDT_A_CLOCKDIVIDER_32K);//约3s溢出

    //Stop WDT
    WDT_A_hold(WDT_A_BASE);

    //调高电源，为高时钟频率做准备
    PMM_setVCore(PMM_CORE_LEVEL_3);

    //设置ACLK的时钟源为内部低速时钟源
    UCS_initClockSignal(
            UCS_ACLK,
            MSP430_CRYSTALS_SEL,
            UCS_CLOCK_DIVIDER_1 );
    //设置FLLREF的时钟源为内部低速时钟源
    UCS_initClockSignal(
            UCS_FLLREF,
            MSP430_CRYSTALS_SEL,
            UCS_CLOCK_DIVIDER_1 );

    //告诉设备使用的外部晶振频率
    UCS_setExternalClockSource(32768,0);
    //设置FLL（关系到主时钟频率）默认1,048,576HZ
    UCS_initFLLSettle(MSP430_FREQUENCY/1000, MSP430_FREQUENCY/MSP430_CRYSTALS_FREQUENCY);//将主频率设置到25,165,824HZ
    //清除错误标志
    UCS_clearFaultFlag(UCS_XT1LFOFFG);

    clockFrequencySMCLK = UCS_getSMCLK();

    msp430_outPutInit();
    __enable_interrupt();
}

void msp430_outPutInit()
{
    /*
     * 配置引脚
     */
    GPIO_setAsOutputPin(MSP430_LED_PORT,MSP430_LED_PIN);
    GPIO_setAsOutputPin(FPGA_POW_PORT,FPGA_POW_PIN);
    GPIO_setAsOutputPin(Spec_USB_POW_PORT,Spec_USB_POW_PIN);
    GPIO_setAsOutputPin(MSP430_PERIPHERAL_MODULE_PORT,MSP430_PERIPHERAL_MODULE_PIN);
    GPIO_setAsOutputPin(ELE_LOCK_PORT,ELE_LOCK_PIN);
    GPIO_setAsOutputPin(WHGM5_POW_PORT,WHGM5_POW_PIN);
    GPIO_setAsOutputPin(TPL_POW_OUT_PORT,TPL_POW_OUT_PIN);

    /*
     * 初始化引脚电平
     */
    //---------------------初始化为高电平------------------------
    GPIO_setOutputHighOnPin(MSP430_LED_PORT,MSP430_LED_PIN);
    GPIO_setOutputHighOnPin(FPGA_POW_PORT,FPGA_POW_PIN);
    GPIO_setOutputHighOnPin(Spec_USB_POW_PORT,Spec_USB_POW_PIN);
    GPIO_setOutputHighOnPin(MSP430_PERIPHERAL_MODULE_PORT,MSP430_PERIPHERAL_MODULE_PIN);
    GPIO_setOutputHighOnPin(WHGM5_POW_PORT,WHGM5_POW_PIN);
    //---------------------初始化为低电平------------------------
    GPIO_setOutputLowOnPin(ELE_LOCK_PORT,ELE_LOCK_PIN);
    GPIO_setOutputLowOnPin(TPL_POW_OUT_PORT,TPL_POW_OUT_PIN);
}

void msp430_LowPower_Enter()
{
    GPIO_setOutputLowOnPin(FPGA_POW_PORT,FPGA_POW_PIN);
    GPIO_setOutputLowOnPin(Spec_USB_POW_PORT,Spec_USB_POW_PIN);
    GPIO_setOutputLowOnPin(MSP430_PERIPHERAL_MODULE_PORT,MSP430_PERIPHERAL_MODULE_PIN);
    GPIO_setOutputLowOnPin(WHGM5_POW_PORT,WHGM5_POW_PIN);
    //关了不用主动打开的
    GPIO_setOutputLowOnPin(TPL_POW_OUT_PORT,TPL_POW_OUT_PIN);//补光灯关
    GPIO_setOutputHighOnPin(MSP430_LED_PORT,MSP430_LED_PIN);//msp430指示灯
    GPIO_setOutputHighOnPin(GPIO_PORT_P2,GPIO_PIN1);//p2.1,关闭GPS模块的状态指示灯

#ifndef __DEBUG
    WDT_A_resetTimer(WDT_A_BASE);//喂狗
    //Stop WDT
    WDT_A_hold(WDT_A_BASE);
#endif
}

void msp430_LowPower_Exit()
{
    GPIO_setOutputHighOnPin(FPGA_POW_PORT,FPGA_POW_PIN);
    GPIO_setOutputHighOnPin(Spec_USB_POW_PORT,Spec_USB_POW_PIN);
    GPIO_setOutputHighOnPin(MSP430_PERIPHERAL_MODULE_PORT,MSP430_PERIPHERAL_MODULE_PIN);
    GPIO_setOutputHighOnPin(WHGM5_POW_PORT,WHGM5_POW_PIN);
    __delay_cycles(MSP430_FREQUENCY*5);

#ifndef __DEBUG
    //Start WDT
    WDT_A_start(WDT_A_BASE);
    WDT_A_resetTimer(WDT_A_BASE);//喂狗
#endif
}

void msp430_HalfSleep_Enter()
{
    GPIO_setOutputLowOnPin(FPGA_POW_PORT,FPGA_POW_PIN);
    GPIO_setOutputLowOnPin(Spec_USB_POW_PORT,Spec_USB_POW_PIN);
    GPIO_setOutputLowOnPin(MSP430_PERIPHERAL_MODULE_PORT,MSP430_PERIPHERAL_MODULE_PIN);
//    GPIO_setOutputLowOnPin(WHGM5_POW_PORT,WHGM5_POW_PIN);
    //关了不用主动打开的
    GPIO_setOutputLowOnPin(TPL_POW_OUT_PORT,TPL_POW_OUT_PIN);//补光灯关
//    GPIO_setOutputHighOnPin(MSP430_LED_PORT,MSP430_LED_PIN);//msp430指示灯
    GPIO_setOutputHighOnPin(GPIO_PORT_P2,GPIO_PIN1);//p2.1,关闭GPS模块的状态指示灯
}

void msp430_HalfSleep_Exit()
{
    GPIO_setOutputHighOnPin(FPGA_POW_PORT,FPGA_POW_PIN);
    GPIO_setOutputHighOnPin(Spec_USB_POW_PORT,Spec_USB_POW_PIN);
    GPIO_setOutputHighOnPin(MSP430_PERIPHERAL_MODULE_PORT,MSP430_PERIPHERAL_MODULE_PIN);
//    GPIO_setOutputHighOnPin(WHGM5_POW_PORT,WHGM5_POW_PIN);
    __delay_cycles(MSP430_FREQUENCY*5);
}

void msp430_TPL_powerOutON()
{
    GPIO_setOutputHighOnPin(TPL_POW_OUT_PORT,TPL_POW_OUT_PIN);
}

void msp430_TPL_powerOutOFF()
{
    GPIO_setOutputLowOnPin(TPL_POW_OUT_PORT,TPL_POW_OUT_PIN);
}
